package com.stone.mythread;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * ThreadLocal : 做线程隔离的，就是为每个线程创建一个全新的对象
 */
public class ThreadLocalTest {

    public static ExecutorService threadPool = Executors.newFixedThreadPool(16);

    public static ThreadLocal<SimpleDateFormat> simpleDateFormatThreadLocal = ThreadLocal.withInitial(() -> new SimpleDateFormat("mm:ss"));

    public static void test1() {
        for (int i = 0; i < 1000; i++) {
            int s = i;
            threadPool.submit(() -> {
                Date date = new Date(1000 * s);
                SimpleDateFormat simpleDateFormat = simpleDateFormatThreadLocal.get();
                String data = simpleDateFormat.format(date);
                System.err.println(data);
            });
        }
        threadPool.shutdown();
    }

    public static void main(String[] args) {
        test1();
    }

}

